/*
        Author: Nathan Newman
        File Name: pcbCode.h
        Description: Header file for pcbCode.c
        Last updated: 9/17/11

*/

#define STACK_SIZE 1024
#define MAX_PRIORITY 127
#define MIN_PRIORITY -128


//ERROR Codes
#define ERR_ALREADY_BLOCKED -7
#define ERR_ALREADY_UNBLOCKED -8
#define ERR_STATE -9

//Enumerated types
enum pcbClass { SYSTEM, APPLICATION }; 
enum pcbState { RUNNING, READY, BLOCKED };
enum qOrdering { PRIORITY, FCFS }; //priority order, first-come-first-serve order

//Process control block struct
typedef struct pcb pcb;
struct pcb { 

	char name[MAX_ARGLEN];
	enum pcbClass class;
	int priority; 
	int isSuspended; //TRUE or FALSE
	enum pcbState state;
	
	//stack
	unsigned char* stackTop;
	unsigned char* stackBase;
	
	//memory
	int memSize;
	unsigned char* loadAddr;
	unsigned char* execAddr;
	
	//place in queue
	struct pcb* frontPtr;
	struct pcb* backPtr;
	
};

//Queue struct
typedef struct { 
	enum qOrdering ordering; //how the q is ordered, e.g. first-come-first-serve or priority
	int size;
	pcb* qFirstPtr;
	pcb* qLastPtr;

} q;

//PERMANENT, internal commands
//Note: all error-returning functions return 0 for success; otherwise, a negative error code is returned
pcb* alloc_pcb(void); //allocate memory for a new pcb and its stack
int free_pcb(pcb*); //free memory associated with pcb, including stack and all
pcb* setup_pcb(char[MAX_ARGLEN],int,enum pcbClass); //initialize pcb content
pcb* find_pcb(char*); //find the pcb with the given name
int insert_pcb(pcb*,q*); //insert the pcb into the given q struct(queue)
int remove_pcb(pcb*); //removes pcb from its current queue

q* getQ(enum pcbClass); //return a pointer to the queue for the given class


